package com.njupt.HashTable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/25 21:12
 * @Description: 128. 最长连续序列
 * @Version: 1.0
 */


public class LongestConsecutive_128 {

    public int longestConsecutive(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.putIfAbsent(nums[i],nums[i]);
        }
        List<Map.Entry<Integer, Integer>> collect = map.entrySet().stream().sorted((e1, e2) -> {
            return e1.getKey().intValue() - e2.getKey().intValue();
        }).collect(Collectors.toList());

        int max = 1;
        int current = 1;
        for (int i = 0; i < collect.size()-1; i++) {
//            int i1 = map.get(Integer.valueOf(i + 1)).intValue();
            if(collect.get(Integer.valueOf(i+1)).getKey().intValue() - collect.get(Integer.valueOf(i)).getKey().intValue() == 1){
                current++;
            }else {
                if(current > max){
                    max = current;
                }

                current = 1;
            }
        }

        return max > current ? max : current;
    }

    public static void main(String[] args) {
//        int[] nums = {1,0,1,2};
        int[] nums = {1};
        LongestConsecutive_128 test = new LongestConsecutive_128();
        System.out.println(test.longestConsecutive(nums));
    }
}
